<?php

class Core_Api_Alias
{

	/**
	 * maxium list of user can created.
	 * @var int
	 */
	CONST QUOTA_ITEM_LIST = 100;
	
	function buildAliasString($string){
		return mb_strtolower(preg_replace("#\s+#", '-', (string)$string));
	}

	function addAlias($resource, $string)
	{
		$string = $this->buildAliasString($string);
		
		if(!$string)
		{
			return ;
		}
		
		$table = Nuo::getTable('core/alias');

		$obj = $table -> find($string) -> current();

		if ($obj)
		{
			return FALSE;
		}

		// delete old item.
		$table -> delete(array(
			"data_type=?" => $resource -> getType(),
			'data_id=?' => $resource -> getId()
		));

		if (!$obj)
		{
			$obj = $table -> fetchNew();
			$obj -> alias_id = $string;
			$obj -> data_type = $resource -> getType();
			$obj -> data_id = $resource -> getId();
			$obj -> data_pk = $resource -> getPk();
			$obj -> save();
		}

		return TRUE;
	}

}
